package com.dnjn.happiness.biz.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.biz.domain.Image;
import com.dnjn.happiness.biz.domain.Moments;
import com.dnjn.happiness.biz.domain.MomentsImage;
import com.dnjn.happiness.biz.mapper.MomentsMapper;
import com.dnjn.happiness.biz.service.IImageService;
import com.dnjn.happiness.biz.service.IMomentsImageService;
import com.dnjn.happiness.biz.service.IMomentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 圈子 服务实现类
 * </p>
 *
 * @author Amber
 * @since 2023-10-04
 */
@Service
public class MomentsServiceImpl extends ServiceImpl<MomentsMapper, Moments> implements IMomentsService {


    @Autowired
    IImageService imageService;
    @Autowired
    IMomentsImageService momentsImageService;

    @Override
    public Moments getOneById(Long id) {
        return baseMapper.selectOneById(id);

    }

    @Override
    public Page<Moments> selectPageX(Moments domain, Page page) {
        return baseMapper.selectPageX(domain, page);
    }


    @Override
    public Moments createDomain(Moments domain) {
        save(domain);
        int i = 1;
        if (Objects.nonNull(domain.getImages())) {
            for (String url : domain.getImages()) {
                Image img = new Image().setImageUrl(url);
                imageService.save(img);
                MomentsImage ximg = new MomentsImage()
                        .setMomentsId(domain.getMomentsId())
                        .setImageId(img.getImageId())
                        .setOrdinal(i++);
                momentsImageService.save(ximg);
            }
        }


        return domain;
    }

    @Override
    public List<String> selectImageUrlByMomentsId(Long momentsId) {
        return momentsImageService.selectImageUrlByMomentsId(momentsId);
    }

    @Override
    public Integer selectCount(Long userId) {
        return baseMapper.selectCount(userId);
    }
}
